<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Storage;
use function Symfony\Component\String\s;


class OrganizationsMembersExamine extends Model
{

    const status1 = '待审核';
    const status2 = '通过';
    const status3 = '拒绝';

    use HasFactory;
    use SoftDeletes;

    static $unguarded = true;

    protected $table = 'organizations_members_examine';

    /**
     * 根据ID获取数据
     *
     * @param $id
     * @param mixed $columns
     * @return array|Builder
     */
    public static function getDataByID($id, string $columns = ''){
        $data = self::query();
        if($columns){
            $data->find($id,$columns);
            return $data;
        }
        $val = $data->find($id);
        $status_show =  self::getStatusValue($val['status']);
        $userInfo = Users::query()->where(['id'=>$val['transfer_id']])->first();
        return [
            'id'=>$val['id'],
            'post_id'=>$val['post_id'],
            'transfer_id'=>$val['transfer_id'],
            'transfer_name'=>$userInfo['name'],
            'status'=>$val['status'],
            'user_id'=>$val['user_id'],
            'status_show'=>$status_show,
//            'status_html_show'=>$status_html_show,
            'created_at'=>Carbon::parse($val['created_at'])->toDateTimeString(),
            'updated_at'=>Carbon::parse($val['updated_at'])->toDateTimeString(),
        ];
    }

    public static function getStatusArray(): array
    {
        return [
            1=>self::status1,
            2=>self::status2,
            3=>self::status3,
        ];
    }

    private static function getStatusValue($status): string
    {
        return self::getStatusArray()[$status];
    }

    public static function countComment($where)
    {
        $data = self::query();
        return $data->where($where)->count();

    }

    public function getCommentLike()
    {

    }

/*    public  function userLike(): \Illuminate\Database\Eloquent\Relations\HasOne
    {
        $user = auth('api')->user();
        $where = array();
        $where['user_id'] = $user['id'];
        $where['status'] = 2;

        return $this->hasOne(CommentLikes::class,'comment_id','id')->where($where);
    }*/

    public function  getList($where,$offset,$limit,$orderby='')
    {

        $data = self::query();

        $data =   $data ->where($where);

        if($orderby  == 'id'){
            $data->oldest($orderby);
        }else{
            $data->latest('id');
        }

        $vals =   $data->offset($offset)->limit($limit)
            ->get();

        return $vals;
    }


    public  static  function  formatListData($data)
    {

        if($data){
            $data = json_encode($data);
            $data = json_decode($data,true);

            $image = $data['image'];
            if($image){
                $image_path = getImageUrl($image);
                $image_show = '<img width="60" height="60" src="'.$image_path.'">';
            }

            $user = auth('api')->user();
            $where = array();
            $where['user_id']  = $user['id'];
            $where['comment_id']  = $data['id'];
            $where['post_id']  = $data['post_id'];
            $count_like = PostsLikes::query()->where($where)->count();


            if($count_like >0 ){
                $data['is_like']   = 2;
            }else{
                $data['is_like']  = 1;
            }

            $wheres = array();
            $wheres['comment_id']  = $data['id'];
            $wheres['post_id']  = $data['post_id'];
            $count_num = PostsLikes::query()->where($wheres)->count();
            $data['count_num'] = $count_num;

            $data['image_path'] = $image_path??"";
            $data['image_show'] = $image_show??"";

            $data['created_at'] = date("Y-m-d H:i:s", strtotime($data['created_at']));
            //添加评论人头像
            $userinfo = Users::getDataByID($data['user_id']);
            $data['avatar']  = $userinfo['avatar'];
            $data['comment_username']  = $userinfo['username'];
        }
        return $data;
    }


    public static function getDataByIDorg($val){

        $val = json_encode($val);
        $val = json_decode($val,true);

        if(empty($val['status'])){
            return $val;
        }

        $status_show = '';
        if($val['status'] == 1){
            $status_show = '待审核';
        }elseif($val['status'] == 2){
            $status_show = '通过';
        }elseif($val['status'] == 3){
            $status_show = '拒绝';
        }

        $image = $val['image'];
        if($image){
            $image_path =getImageUrl($image);
            //$image_path = Storage::url($val['image']);
            $image_show = '<img width="60" height="60" src="'.$image_path.'">';
        }

        $wheres = array();
        $wheres['id'] = $val['post_id'];
       // $postInfo =    Posts::getDataOne($wheres);
        //$postInfo
        $postInfo = Posts::query()->where($wheres)->first();
        return [
            'id'=>$val['id'],
            'post_id'=>$val['post_id'],
            'comment'=>$val['comment'],
            'status'=>$val['status'],
            'user_id'=>$val['user_id'],
            'username'=>$val['username'],
            'remark'=>$val['remark'],
            'status_show'=>$status_show,
            'created_at'=>Carbon::parse($val['created_at'])->toDateTimeString(),
            'updated_at'=>Carbon::parse($val['updated_at'])->toDateTimeString(),
            'title'=>$postInfo['title'],
        ];
    }
}
